CREATE TABLE [dbo].[StarProfile](
	[StarProfileId] [int] IDENTITY(1,1) NOT NULL,
	[FirstName] [varchar](100) NOT NULL,
	[LastName] [varchar](100) NOT NULL,
	[Gender] [varchar](10) NOT NULL,
	[Ethnicity] [varchar](50) NULL,
	[CountryOfOrigin] [varchar](50) NULL,
	[State] [varchar](50) NULL,
	[PlaceOfBirth] [varchar](50) NULL,
	[City] [varchar](50) NULL,
	[DateOfBirth] [datetime] NULL,
	[EyeColor] [varchar](50) NULL,
	[HairColor] [varchar](50) NULL,
	[Height] [varchar](50) NULL,
	[Weight] [varchar](50) NULL,
	[MyTwitter] [varchar](500) NULL,
	[WebsiteTwitter] [varchar](500) NULL,
 CONSTRAINT [PK_StarProfile] PRIMARY KEY CLUSTERED 
(
	[StarProfileId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[StarProfilePhoto](
	[StarProfilePhotoId] [int] IDENTITY(1,1) NOT NULL,
	[StarProfileId] [int] NOT NULL,
	[StarProfilePhotoTitle] [varchar](100) NOT NULL,
	[StarProfilePhotoFileName] [varchar](255) NOT NULL,
	[IsActive] [bit] NOT NULL,
 CONSTRAINT [PK_StarProfilePhoto] PRIMARY KEY CLUSTERED 
(
	[StarProfilePhotoId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[StarProfilePhoto]  WITH CHECK ADD  CONSTRAINT [FK_StarProfilePhoto_StarProfile] FOREIGN KEY([StarProfileId])
REFERENCES [dbo].[StarProfile] ([StarProfileId])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[StarProfilePhoto] CHECK CONSTRAINT [FK_StarProfilePhoto_StarProfile]
GO
CREATE TABLE [dbo].[StarProfileScene](
	[StarProfileSceneId] [int] IDENTITY(1,1) NOT NULL,
	[StarProfileId] [int] NOT NULL,
	[StarProfileSceneTitle] [varchar](100) NULL,
	[PreviewFileName] [varchar](255) NULL,
	[HDWinFileName] [varchar](255) NULL,
	[LDWinFileName] [varchar](255) NULL,
	[HDQTFileName] [varchar](255) NULL,
	[LDQTFileName] [varchar](255) NULL,
	[Poster] [varchar](255) NULL,
	[IsActive] [bit] NOT NULL,
 CONSTRAINT [PK_StarProfileScene] PRIMARY KEY CLUSTERED 
(
	[StarProfileSceneId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[StarProfileScene]  WITH CHECK ADD  CONSTRAINT [FK_StarProfileScene_StarProfile] FOREIGN KEY([StarProfileId])
REFERENCES [dbo].[StarProfile] ([StarProfileId])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[StarProfileScene] CHECK CONSTRAINT [FK_StarProfileScene_StarProfile]
GO
ALTER PROCEDURE [dbo].[spStarProfile_Search]
(
	@Name varchar(500) = null,
	@Gender varchar(10) = null, 
	@Rank int = null
)  
AS
begin
	select sp.StarProfileId, sp.FirstName, sp.LastName, sp.Gender, 
		sp.Ethnicity, sp.CountryOfOrigin, sp.State, sp.PlaceOfBirth, sp.City, sp.DateOfBirth, 
		sp.EyeColor, sp.HairColor, sp.Height, sp.Weight, sp.MyTwitter, sp.WebsiteTwitter,
		spp.CountPhoto, sps.CountScene, 5 as StarRank
    from StarProfile (nolock) sp left join (select StarProfileId, count(*) AS CountPhoto
											from StarProfilePhoto (nolock)
											group by StarProfileId) as spp on sp.StarProfileId = spp.StarProfileId
								left join (select StarProfileId, count(*) AS CountScene 
											from StarProfileScene (nolock)
											group by StarProfileId) as sps on sp.StarProfileId = sps.StarProfileId
    where ((sp.FirstName + ' ' + sp.LastName LIKE '%' + @Name + '%') or (@Name is null))
		and ((sp.Gender = @Gender) or (@Gender is null))
end
GO